<?php
class DomLockerTestCase extends UnitTestCase {
	
	public function testLocker() {
		$locker = new DomLocker( 'test' );
		$this->assertFalse( $locker->isLocked() );
		
		$locker->lock();
		$this->assertTrue( $locker->isLocked() );
		
		$locker->unlock();
		$this->assertFalse( $locker->isLocked() );

	}

	public function testWaitLock() {
		$locker = new DomLocker( 'test' );
		$locker->setWaitTime( 1 );
		$locker->setResetTime( 2 );
		$this->assertFalse( $locker->isLocked() );
		
		$locker->lock();
		$this->assertTrue( $locker->isLocked() );
		
		
		// wait for lock and hope to get exception, that lock is on.
		try {
			$time = microtime(true);
			$locker->wait();
			$this->assertFalse(true, 'Locker didn\'t wait for lock to be realeased');
		} catch( LockTimeExceededException $e ) {
			$endTime  = microtime(true);
			$this->assertTrue( $endTime - $time > 0.9 );
			$this->assertTrue( $locker->isLocked() );	
		}
		
		// lock should be automatically removed after 3 seconds
		sleep(2);
		$this->assertFalse( $locker->isLocked() );
		
		// release it anyways.. even if there was problems.
		$locker->unlock();
	}
	
}
?>